package com.prothink.threadproject.task;

import com.prothink.threadproject.entity.FormLogin;
import com.prothink.threadproject.entity.User;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.concurrent.ExecutorService;

public class SendMailTask implements Runnable{
    private JavaMailSenderImpl mailSender;
    private FormLogin user;
    private String password;

    public SendMailTask(JavaMailSenderImpl mailSender, FormLogin user, String password){
        this.mailSender = mailSender;
        this.user = user;
        this.password = password;
    }

    @Override
    public void run() {
        try {
            //创建复杂邮件的对象
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            //设置标题
            mimeMessageHelper.setSubject("重置密码");
            String htmlText = "<table><tr><th>账号：</th><th>密码：</th></tr><tr><td>%s</td><td>%s</td></tr></table>";
            htmlText = String.format(htmlText, user.getAccount(), password);
            mimeMessageHelper.setText(htmlText, true);

            //设置邮件附件 文件名称有中文可能会乱码
            File file = new File("/Users/mayanping/Desktop/进思/2309-C/2309-C周考-20240920/日测.zip");
            mimeMessageHelper.addAttachment("2309-C日测题-乱码", file);

            // 使用 encodeText、encodeWord 必须加上不截取配置
            System.getProperties().setProperty("mail.mime.splitlongparameters", "false");

            mimeMessageHelper.addAttachment(MimeUtility.encodeWord("2309-C日测题", "UTF-8", "B"), file);
            mimeMessageHelper.setTo(user.getEmail());
            mimeMessageHelper.setFrom("yuddpky@163.com");

            mailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
